<?php

class ES_IndexService extends ES_ESService
{
    private static $self = NULL;

    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }

    /**
     * 获取节点信息
     */
    public function getESInfo()
    {
        return  ES_ESService::$es_db->info();
    }

    /**
     * 手动创建索引
     * @param $params
     *
     * $params['index']                 =  (string) 索引名
     *        ['body']['settings']['number_of_shards']      = (number) 主分片数
     *        ['body']['settings']['number_of_replicas']    = (number) 复制分片数
     *
     *        ['body']['mappings']['_default_']['_source']['enabled']    = (boolean)是否开启_source字段
     *        ['body']['mappings']['_default_']['properties']            = array(
     *                  'title'     => array(
     *                      'type'      => 'string',
     *                      'analyzer'  => 'standard'
     *              )
     *          )
     * @return array
     */
    public function createESIndex($params)
    {
       return  ES_ESService::$es_db ->indices()->create($params);
    }

    /**
     * 手动创建索引
     * @param $params
     * $deleteParams['index']   =  (string) 索引名
     * @return array
     */
    public function deleteESIndex($params)
    {
        return  ES_ESService::$es_db ->indices()->delete($params);
    }

    /**
     * 获取索引配置信息
     * @param $params
     * $params['index'] = 'my_index';    单条索引信息
     * $params['index'] = array('my_index', 'my_index2'); 多条索引信息
     * @return array
     */
    public function getSetting($params)
    {
        return  ES_ESService::$es_db ->indices()->getSettings($params);
    }

    /**
     * 设置索引配置信息
     * @param $params
     * $params['body']['index']['number_of_replicas'] = 5;  修改复制分片数
     * @return array
     */
    public function putSetting($params)
    {
        return  ES_ESService::$es_db ->indices()->putSettings($params);
    }

    /**
     * 获取Mapping信息
     * @param $params
     * $params['index'] = 'my_index';
     * $params['type']  = 'my_type'
     * @return array
     */
    public function getMapping($params)
    {
        return  ES_ESService::$es_db ->indices()->getMapping($params);
    }

    /**
     * 配置Mapping信息
     * @param $params
     * $indexParams['index']  = 'udo_index';
     * $indexParams['type']   = 'udo_type';
     * $mapping = array(
     *      '_source' => array(
     *         'enabled' => true
     *      ),
     *      'properties' => array(
     *          'first_name' => array(
     *              'type' => 'string',
     *              'analyzer' => 'standard'
     *          ),
     *          'last_name' => array(
     *              'type' => 'string',
     *              'analyzer' => 'standard'
     *          ),
     *          'age' => array(
     *              'type' => 'integer'
     *          )
     *      )
     *  );
     *  $indexParams['body']['udo_type'] = $mapping;
     * @return array
     */
    public function putMapping($params)
    {
        return  ES_ESService::$es_db ->indices()->putMapping($params);
    }

    /**
     * 添加单条数据
     * @param $params
     * $params['index']             = 'my_index';
     * $params['type']              = 'my_type';
     * $params['id']                = 'my_id'; (可选)
     * $params['body']['myField']   = 'abc';
     * @return array
     */
    public function insertESData($params)
    {
        return  ES_ESService::$es_db ->index($params);
    }

    public function getInsertId($params,$primaryId)
    {
        $indexParams = array();
        $indexParams['index']             = $params['index'];
        $indexParams['type']              = $params['type'];
        $indexParams['body']['size']              = 0;
        $indexParams['body']['aggs']['max_id']['max']['field'] = $primaryId;
        $result = ES_ESService::$es_db ->search($indexParams);
        return $result['aggregations']['max_id']['value'] + 1;
    }

    /**
     * 批量操作数据
     * @param $params
     * @return array
     */
    public function bulkESData($params)
    {

        return  ES_ESService::$es_db ->bulk($params);
    }

    /**
     * 更新单条数据
     * @param $params
     * @return array
     */
    public function updateESData($params)
    {
        return  ES_ESService::$es_db ->update($params);
    }

    /**
     * 搜索数据
     * @param $params
     * @return array
     */
    public function searchESData($params)
    {
        return ES_ESService::$es_db->search($params);
    }

    /**
     * 获取模版
     * @param $params
     * @return array
     */
    public function getTemplate($params)
    {
        return ES_ESService::$es_db->indices()->getTemplate($params);
    }

    /**
     * 设置模版
     * @param $params
     * @return array
     */
    public function putTemplate($params)
    {
        return ES_ESService::$es_db->indices()->putTemplate($params);
    }

    public function snapshot()
    {
        return ES_ESService::$es_db->snapshot();
    }

    /**
     * 删除数据
     * @param $params
     * @return array
     */
    public function delete($params)
    {
        return ES_ESService::$es_db->delete($params);
    }

    /**
     * 根据条件删除数据
     * @param $params
     * @return array
     */
    public function deleteByQuery($params)
    {
        return ES_ESService::$es_db->deleteByQuery($params);
    }


    public function getESData()
    {
//        $db_info = self::getIndex(ServiceTypeEnum::TYPE_EXERCISE_ITEM);
        $db_info = self::getIndex(ServiceTypeEnum::TYPE_TEST_DATA);
        $params = array();
        $params['index'] = $db_info['index'];
        $params['type'] = $db_info['type'];
//        self::printfJson($params);
//        $result = ES_ESService::$es_db->search($params);
        $result = ES_ESService::$es_db->info();
        echo "<pre>";
        dd($result);
    }






}



